<template>
	<view class="friends-page">
		<!-- 页面标题 -->
		<view class="page-header">
			<text class="page-title">我的好友</text>
		</view>

		<!-- 好友列表 -->
		<view class="friends-list">
			<view class="friend-item" v-for="(friend, index) in friendsList" :key="index">
				<image :src="friend.avatarUrl || '/static/images/user-avatar.png'" class="friend-avatar"></image>
				<view class="friend-info" @tap="goToChat(friend)">
					<text class="friend-name">{{ friend.nickName || '用户' }}</text>
					<text class="friend-location">{{ friend.province || '' }} {{ friend.city || '' }}</text>
				</view>
				<view class="friend-actions">
					<text class="friend-action chat-action" @tap="goToChat(friend)">聊天</text>
					<text class="friend-action delete-action" @tap="deleteFriend(friend)">删除</text>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view v-if="friendsList.length === 0 && !loading" class="empty-state">
				<text class="empty-text">暂无好友</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue';
	import { getAllFriends, deleteFriend as deleteFriendApi } from '@/api/friend.js';
	import userManager from '@/common/utils/user.js';

	const friendsList = ref([]);
	const loading = ref(false);
	const userId = ref('');

	onMounted(() => {
		// 获取当前用户ID
		const userInfo = userManager.getUserInfo();
		if (userInfo && userInfo.userId) {
			userId.value = userInfo.userId;
			loadFriends();
		}
	});

	// 加载好友列表
	const loadFriends = async () => {
		if (!userId.value) {
			uni.showToast({
				title: '用户未登录',
				icon: 'none'
			});
			return;
		}

		loading.value = true;
		try {
			const res = await getAllFriends(userId.value);
			if (res.data.code === 200) {
				friendsList.value = res.data.data || [];
			} else {
				uni.showToast({
					title: res.data.message || '获取好友列表失败',
					icon: 'none'
				});
			}
		} catch (error) {
			console.error('获取好友列表失败:', error);
			uni.showToast({
				title: '网络请求失败',
				icon: 'none'
			});
		} finally {
			loading.value = false;
		}
	};

	// 跳转到聊天页面
	const goToChat = (friend) => {
		// 跳转到聊天页面，传递好友信息
		uni.navigateTo({
			url: `/pages/chat/chat?id=${friend.friendUserId}&name=${encodeURIComponent(friend.nickName)}&avatar=${encodeURIComponent(friend.avatarUrl)}&chatType=friend`
		});
	};

	// 删除好友
	const deleteFriend = (friend) => {
		uni.showModal({
			title: '确认删除',
			content: `确定要删除好友 ${friend.nickName} 吗？`,
			success: async (res) => {
				if (res.confirm) {
					try {
						const result = await deleteFriendApi(userId.value, friend.friendUserId);
						if (result.data.code === 200) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
							// 从列表中移除该好友
							friendsList.value = friendsList.value.filter(f => f.friendUserId !== friend.friendUserId);
						} else {
							uni.showToast({
								title: result.data.message || '删除失败',
								icon: 'none'
							});
						}
					} catch (error) {
						console.error('删除好友失败:', error);
						uni.showToast({
							title: '删除失败',
							icon: 'none'
						});
					}
				}
			}
		});
	};
</script>

<style scoped>
	.friends-page {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx;
	}

	.page-header {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
	}

	.page-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.friends-list {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
	}

	.friend-item {
		display: flex;
		align-items: center;
		padding: 30rpx 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.friend-item:last-child {
		border-bottom: none;
	}

	.friend-avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.friend-info {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.friend-name {
		font-size: 30rpx;
		color: #333;
		margin-bottom: 10rpx;
	}

	.friend-location {
		font-size: 24rpx;
		color: #999;
	}

	.friend-actions {
		display: flex;
		flex-direction: column;
		gap: 10rpx;
	}

	.friend-action {
		font-size: 24rpx;
		padding: 8rpx 16rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.chat-action {
		color: #89CFF0;
		border: 1rpx solid #89CFF0;
	}

	.delete-action {
		color: #FF3B30;
		border: 1rpx solid #FF3B30;
	}

	.empty-state {
		text-align: center;
		padding: 100rpx 0;
	}

	.empty-text {
		font-size: 28rpx;
		color: #999;
	}
</style>